<template>
    <div class="back">
        <div class="login">
            <h2>叩丁狼商品管理系统</h2>
            <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="请输入用户名">
                        <template #prefix>
                            <el-icon :size="24">
                                <UserFilled />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="ruleForm.password" type="password" placeholder="请输入密码">
                        <template #prefix>
                            <el-icon :size="24">
                                <Lock />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)" size="large">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { UserFilled, Lock } from "@element-plus/icons-vue"
import { ElMessage } from "element-plus"
import { loginApi } from "@/views/login/api"
import { useRouter } from 'vue-router'

const router = useRouter();
const ruleFormRef = ref<FormInstance>()
const validatePass = (rule: any, value: string, callback: any) => {
    if (value === '') {
        // 如果校验不通过，人家规定我们要调用 callback，传入一个error对象，把信息传入error对象
        callback(new Error('请输入密码'))
    } else {
        // 校验成功，就直接调用callback就行
        callback()
    }
}

const ruleForm = reactive({
    username: '',
    password: '',
})

const rules = reactive({
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' },],
    password: [{ validator: validatePass, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        // valid是true代表数据校验难过，可以发请求
        if (valid) {
            // console.log('submit!')
            loginApi(ruleForm).then((res) => {
                if (res.code === 200) {
                    // 登录成功
                    // 存token
                    localStorage.setItem("token", res.data.tokenHead + res.data.token)
                    // 跳转
                    router.push("/index")
                    // 提示
                    ElMessage.success("登录成功")
                }
            })
        } else {
            // 校验失败，提示，返回false
            ElMessage.error('加钱优化')
            return false
        }
    })
}
</script>

<style lang="scss" scoped>
.back {
    background-color: #2d3a4b;
    height: 100%;
    width: 100%;
    display: flex;
    margin: auto;

    .login {
        width: 400px;
        margin: auto;

        :deep(.el-button--primary) {
            width: 100%;
        }

        :deep(.el-input--prefix) {
            height: 50px;

        }

        h2 {
            text-align: center;
            color: #fff;
            padding: 20px 0;
        }
    }
}
</style>